﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ServiceConsumer;

/// <summary>
/// Summary description for BrokerServices
/// </summary>
public class BrokerServices
{
    private int iBrokerID;
    private string sBrokerUserName;
    private string sBrokerPassword;
    private string sUrlWS;
    private string sWSLogin;
    private string sWSTranferMoneyCustomer;
    private string sWSTranferMoneyProvider;
    
	public BrokerServices()
	{
        NhaKinhDoanhDataContext db = new NhaKinhDoanhDataContext();

        var query = (from nmg in db.NhaMoiGiois
                     where nmg.DefaultBroker == 1
                     select nmg).Single();

        sUrlWS = query.LinkWS;
        sBrokerUserName = query.UserName;
        sBrokerPassword = query.PassWord;
        sWSLogin = query.LinkWS_Login;
        sWSTranferMoneyCustomer = query.LinkWS_TranferMoneyForCustomer;
        sWSTranferMoneyProvider = query.LinkWS_TranferMoneyForProvider;

        iBrokerID = query.BrokerID;
	}

    public BrokerServices(int BrokerID)
    {
        iBrokerID = BrokerID;
        NhaKinhDoanhDataContext db = new NhaKinhDoanhDataContext();

        var query = (from nmg in db.NhaMoiGiois
                     where nmg.BrokerID == 1
                     select nmg).Single();

        sUrlWS = query.LinkWS;
        sBrokerUserName = query.UserName;
        sBrokerPassword = query.PassWord;
        sWSLogin = query.LinkWS_Login;
        sWSTranferMoneyCustomer = query.LinkWS_TranferMoneyForCustomer;
        sWSTranferMoneyProvider = query.LinkWS_TranferMoneyForProvider;

        //iBrokerID = query.BrokerID;
    }

    public string WSLogin()
    {
        try
        {
            List<object> lstParas = new List<object>();
            lstParas.Add(sBrokerUserName);
            lstParas.Add(sBrokerPassword);

            object oSID = WSProxy.CallWebService(sUrlWS, "WSLogin", lstParas.ToArray());
            string sSID = oSID.ToString();

            return sSID;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    public string TranferMoneyForCustomer(string sCustomerCreditCard, float fAmountMoney, string sSecureNum, string sHolderName)
    {
        try
        {
            List<object> lstParas = new List<object>();
            
            lstParas.Add(WSLogin());

            lstParas.Add(sCustomerCreditCard);
            lstParas.Add(fAmountMoney);

            string[] arr = sWSTranferMoneyCustomer.Split('|');

            if (int.Parse(arr[1]) == 5)
            {
                lstParas.Add(sSecureNum);
                lstParas.Add(sHolderName);
            }

            object oSID = WSProxy.CallWebService(sUrlWS, arr[0].Trim(), lstParas.ToArray());
            string sSID = oSID.ToString();

            return sSID;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    public string TranferMoneyForProvider(string sCustomerCreditCard, float fAmountMoney, string sSecureNum, string sHolderName)
    {
        try
        {
            List<object> lstParas = new List<object>();

            lstParas.Add(WSLogin());

            lstParas.Add(sCustomerCreditCard);
            lstParas.Add(fAmountMoney);

            string[] arr = sWSTranferMoneyProvider.Split('|');

            if (int.Parse(arr[1]) == 5)
            {
                lstParas.Add(sSecureNum);
                lstParas.Add(sHolderName);
            }

            object oSID = WSProxy.CallWebService(sUrlWS, arr[0].Trim(), lstParas.ToArray());
            string sSID = oSID.ToString();

            return sSID;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
}